
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title> 方剂编辑 </title>
    <link rel="stylesheet" type="text/css" href="/css/style.css" />

    <style type="text/css">
        *, body {
            margin: 0px;
            font-family: '微软雅黑';
        }
    </style>
    <script src="~/js/jquery-1.10.2.min.js"></script>
    <script src="~/js/jquery.form.js"></script>
    <script src="~/js/generic.js"></script>
</head>

<body>

@Html.Partial("top")

<table class="maintable">
    <tr>
        @Html.Partial("menu")
        <td class="rightmaintd" valign="top">
            <div class="searchdiv">
                <a href="/fjgl/list?nianji=@ViewBag.NainJi&company=@ViewBag.Company&currPage=@ViewBag.CurrPage">&lt; 返回</a>
                <div class="rightsearchdiv"><!-- name --></div>
            </div>
            <table class="leftrighttable" border="0">
                <tr>
                    <td class="lefttdtable"  valign="top" style="width:50%">
                        <table cellpadding="6" align="left">
                            <tr>
                                <td class="tdname">方剂名</td>
                                <td><input type="text" onblur="validateagain();" id="jingfangming" class="textinput" maxlength="50" style="width:300px"></td>
                            </tr>
                            <tr>
                                <td class="tdname">来源</td>
                                <td><input type="text" id="laiyuan" class="textinput" maxlength="50" style="width:300px"></td>
                            </tr>
                            <tr>
                                <td class="tdname">类型</td>
                                <td>
                                    <select id="leixing" class="textselect" style="width:300px">
                                        <option value="">请选择</option>
                                        <option value="经方">经方</option>
                                        <option value="时方">时方</option>
                                        <option value="验方">验方</option>
                                        <option value="自拟方">自拟方</option>
                                    </select>
                                </td>
                            </tr>
                            <tr>
                                <td class="tdname" valign="top">剂型</td>
                                <td>
                                    <select id="jixing" class="textselect" style="width:300px">
                                        <option value="">请选择</option>
                                        <option value="汤">汤</option>
                                        <option value="丸">丸</option>
                                        <option value="片">片</option>
                                        <option value="散">散</option>
                                        <option value="膏">膏</option>
                                        <option value="丹">丹</option>
                                        <option value="茶">茶</option>
                                        <option value="露">露</option>
                                        <option value="锭">锭</option>
                                        <option value="酒">酒</option>
                                        <option value="条">条</option>
                                        <option value="线">线</option>
                                        <option value="洗">洗</option>
                                        <option value="搽">搽</option>
                                        <option value="栓">栓</option>
                                        <option value="颗粒">颗粒</option>
                                        <option value="冲剂">冲剂</option>
                                        <option value="胶囊">胶囊</option>
                                        <option value="糖浆">糖浆</option>
                                        <option value="口服液">口服液</option>
                                        <option value="其他">其他</option>
                                    </select>
                                </td>
                            </tr>
                            <tr>
                                <td class="tdname" valign="top">方论</td>
                                <td><textarea id="fanglun" rows="" cols="" class="textareainput" style="width:300px;height:130px"></textarea></td>
                            </tr>
                            <tr>
                                <td class="tdname" valign="top">用法用量</td>
                                <td> <textarea id="yongfayongliang" rows="" cols="" class="textareainput" style="width:300px;height:130px"></textarea></td>
                            </tr>
                            <tr>
                                <td class="tdname" style="height: 50px;">&nbsp;</td>
                                <td>
                                    <input type="radio" id="jinzhi" name="peifang3" value="今制" checked="checked">今制  <input type="radio" id="guzhi" name="peifang3" value="古制">古制
                                </td>
                            </tr>
                        </table>
                    </td>
                    <td class="righttdtable"  valign="top" style="width:50%">
                        <table cellpadding="6" align="left" >
                            <tr>
                                <td class="tdname">方剂名医</td>
                                <td><input type="text" id="fangjimingyi" class="textinput" maxlength="50" style="width:300px"></td>
                            </tr>
                            <tr>
                                <td class="tdname" valign="top">名医朝代</td>
                                <td>
                                    <select id="chaodai" class="textselect" style="width:300px">
                                        <option value="">请选择</option>
                                        <option value="1979年后">1979年后</option>
                                        <option value="1949-1979年">1949-1979年</option>
                                        <option value="民国">民国</option>
                                        <option value="清">清</option>
                                        <option value="明">明</option>
                                        <option value="元">元</option>
                                        <option value="宋">宋</option>
                                        <option value="隋唐五代">隋唐五代</option>
                                        <option value="魏晋南北朝">魏晋南北朝</option>
                                        <option value="汉">汉</option>
                                        <option value="先秦">先秦</option>
                                        <option value="上古">上古</option>
                                    </select>
                                </td>
                            </tr>
                            <tr>
                                <td class="tdname" valign="top">名医流派</td>
                                <td>
                                    <select id="liupai" class="textselect" style="width:300px">
                                        <option value="">请选择</option>
                                        <option value="伤寒派">伤寒派</option>
                                        <option value="脾胃派">脾胃派</option>
                                        <option value="滋阴派">滋阴派</option>
                                        <option value="寒凉派">寒凉派</option>
                                        <option value="温补派">温补派</option>
                                        <option value="温病派">温病派</option>
                                        <option value="火神派">火神派</option>
                                    </select>
                                </td>
                            </tr>
                            <tr>
                                <td class="tdname" valign="top">功能</td>
                                <td><textarea id="gongneng" rows="" cols="" class="textareainput" style="width:300px"></textarea></td>
                            </tr>
                            <tr>
                                <td class="tdname" valign="top">主治病证/病机</td>
                                <td> <textarea id="zhuzhi" rows="" cols="" class="textareainput" style="width:300px"></textarea></td>
                            </tr>
                            <tr>
                                <td class="tdname" valign="top">主治症状</td>
                                <td> <textarea id="zhuzhizhengzhuang" rows="" cols="" class="textareainput" style="width:300px"></textarea></td>
                            </tr>
                            <tr>
                                <td class="tdname">禁忌</td>
                                <td><input type="text" id="jinji" class="textinput" maxlength="50" style="width:300px"></td>
                            </tr>
                        </table>
                    </td>
                </tr>
                <tr>
                    <td  valign="top" style="padding-top:21px">
                        @* <div style="clear:both; border:0px solid red; position: relative ;">*@
                        <table border="1">
                            <tr >
                                <td class="tdname" style="height: 50px;width: 80px; ">中药名&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
                                <td >
                                    <input type="text" name="" style="border:1px solid #ccc; width:160px;height:24px;line-height:150%;" onkeyup="selectz();" id="peifang1">

                                    <div id='selectZZ' style="width: 160px;display: none; border: 1px solid #ddd; position: relative; left: 0px; top: 0px; z-index: 11; background: #fff;">
                                    </div>
                                </td>
                            </tr>
                            <tr style="height: 30px;">
                                <td class="tdname" style="height: 50px; width: 80px;">药量&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
                                <td>
                                    <input type="text" name="" style="border:1px solid #ccc; width:60px;height:24px;line-height:150%;" id="peifang2">
                                    参考量:<input type="text" name="" style="border:1px solid #ccc; width:60px;height:24px;line-height:150%;" id="cankaoyongliang">
                                    @*<input type="button" value="添 加" class="tianjiabtn" onclick="add();" >*@
                                </td>
                                <td align="right"style="width:200px">
                                    <input type="button" value="添 加=>" class="tianjiabtn" onclick="add();">
                                </td>
                            </tr>
                            <tr>
                                <td class="tdname" style="height: 50px;">&nbsp;</td>
                                <td>

                                </td>
                            </tr>
                        </table>
                        @*</div>*@
                    </td>
                    <td>
                        <div>
                            &nbsp; 总药味: <span id="spnumber"></span>
                        </div>
                        <div class="tianjiafangjidiv" id="divPeifang" style="width: 460px; height: 150px;">

                        </div>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <div class="shenhediv" style="margin-left: 10px;">
                            <input type="submit" value="保 &nbsp; &nbsp; 存" class="baocunbtn" onclick="saveInfo()">
                            <input type="button" value="审核通过" style="display: none;" id="btnValidate" class="shenhebtn" onclick="validate();"> &nbsp;&nbsp;
                            <span id="lrxx"></span>
                        </div>
                    </td>
                </tr>
            </table>



        </td>
    </tr>

</table>
@Html.Partial("bottom")
</body>
</html>
<script type="text/javascript">
    var usertype = "@type";
    var _app_guid = '@ViewBag.Guid';
    var _app_edit = '@ViewBag.EditState';
    var list = [];
    var submit = true;
    function validateagain() {
        if (_app_edit == "edit")
            return;
        util.getJSON("/fjgl/GetName", { "name": $("#jingfangming").val() }, function (json) {
            if (json.status == 1) {
                alert(json.msg);
            }
        });
    }
    function selectz() {
        util.getJSON("/zyxx/GetNames", { "name": $("#peifang1").val() }, function (json) {
            var ret = "";
            for (var i = 0; i < json.list.length; i++) {
                ret += "<p><a onclick='selectedzz(\"" + json.list[i].yaoming + "\")'>" + json.list[i].yaoming + "</a></p>";
            }
            $("#selectZZ").html(ret);
            $("#selectZZ").show();
        });
    }
    function selectedzz(te) {
        $("#peifang1").val(te);
        $("#selectZZ").hide();
    }
    $(document).ready(function () {
        if ((usertype == "管理员" || usertype == "审核者" || usertype == "教师审核") && _app_edit == "edit") {
            $("#btnValidate").show();
        }

        if (_app_edit == "edit") {
            util.getJSON("/fjgl/Get", { "id": _app_guid }, function (json) {
                if (json.status == 0) {
                    $("#lrxx").html('(录入人：' + json.entity.createuser + '， 录入时间：' + util.formateDate(json.entity.createtime, "yyyy-MM-dd hh:mm:ss") + ') ');
                    if (json.entity.shenhe && json.entity.shenhe == "通过") {
                        $("#btnValidate").val("已通过审核");
                    }
                    $("#jingfangming").val(json.entity.jingfangming);
                    $("#laiyuan").val(json.entity.laiyuan);
                    $("#leixing").val(json.entity.leixing);
                    $("#jixing").val(json.entity.jixing);
                    $("#fanglun").val(json.entity.fanglun);

                    $("#fangjimingyi").val(json.entity.fangjimingyi);
                    $("#chaodai").val(json.entity.chaodai);
                    $("#liupai").val(json.entity.liupai);
                    $("#gongneng").val(json.entity.gongneng);
                    if (json.entity.jinguzhi == "古制") {
                        $("#guzhi").attr("checked", true);
                    }

                    $("#zhuzhi").val(json.entity.zhuzhi);
                    $("#zhuzhizhengzhuang").val(json.entity.zhuzhizhengzhuang);
                    $("#yongfayongliang").val(json.entity.yongfayongliang);
                    $("#jinji").val(json.entity.jinji);
                    if (json.list) {
                        for (var i = 0; i < json.list.length; i++) {
                            list.push({ "jingfang": _app_guid, "yaowu": json.list[i].yaowu, "yongliang": json.list[i].yongliang, "cankaoyongliang": json.list[i].cankaoyongliang, "fenlei": json.list[i].fenlei, "sort": i + 1 });
                        }
                    }
                    list = json.list;
                    getZhenzhuang();
                }
            });
            $("#divTitle").html("更新方剂信息");

        } else {
            $("#divTitle").html("创建方剂信息");
        }
    });

    //保存
    function saveInfo() {
        if ($("#jingfangming").val() == "") {
            alert("请输入方剂名");
            $("#jingfangming").focus();
            return;
        }
        var entity = {
            "jingfangming": $("#jingfangming").val(),
            "laiyuan": $("#laiyuan").val(),
            "leixing": $("#leixing").val(),
            "jixing": $("#jixing").val(),
            "fanglun": $("#fanglun").val(),
            "fangjimingyi": $("#fangjimingyi").val(),
            "chaodai": $("#chaodai").val(),
            "liupai": $("#liupai").val(),
            "gongneng": $("#gongneng").val(),
            "zhuzhi": $("#zhuzhi").val(),
            "zhuzhizhengzhuang":$("#zhuzhizhengzhuang").val(),
            "yongfayongliang": $("#yongfayongliang").val(),
            "jinji": $("#jinji").val(),
            "jinguzhi": $("input[name='peifang3']:checked").val()

        };
        var url = "/fjgl/Create";
        if (_app_edit == "edit") {
            url = "/fjgl/Update";
            entity.id = _app_guid;
        }

        util.postJson(url,
            {
                "entity": entity,
                "list": list
            }, function(json) {
                if (json.status == 0) {
                    alert(json.msg);
                    location.href = "/fjgl/List";
                } else {
                    alert(json.msg);
                }
            });
    }

    function add() {
        if ($("#peifang1").val() != "") {
            var exist = false;
            var peifang2 = $("#peifang2").val();
            if (!isNaN(peifang2)) {
                peifang2 = peifang2 + "克";
            }
            var cankaoyongliang = $("#cankaoyongliang").val();
            if (!isNaN(cankaoyongliang)) {
                cankaoyongliang = cankaoyongliang;
            }
            for (var i = 0; i < list.length; i++) {
                if (list[i].yaowu == $("#peifang1").val()) {
                    list[i].yaowu = $("#peifang1").val();
                    list[i].yongliang = peifang2;
                    list[i].cankaoyongliang = cankaoyongliang;
                    exist = true;
                    break;
                }
            }
            if (!exist) {
                var sort = 0;
                if (list.length > 0) {
                    sort = list[list.length - 1].sort+1;
                }
                list.push({ "jingfang": _app_guid, "yaowu": $("#peifang1").val(), "yongliang": peifang2,"cankaoyongliang":cankaoyongliang, "sort": sort });
            }
            $("#peifang1").val("");
        } else {
            alert("请输入药名");
        }
        getZhenzhuang();
    }

    function del(yaowu, yongliang) {
        var retList = [];
        for (var i = 0; i < list.length; i++) {
            if (list[i].yaowu == yaowu && list[i].yongliang == yongliang) {
                continue;
            }
            retList.push(list[i]);
        }
        list = retList;
        getZhenzhuang();
    }

    function getZhenzhuang() {
        $("#divPeifang").html("");
        if (list) {
            var html = "";
            for (var i = 0; i < list.length; i++) {
                html += '<span class="fangjispan">' + list[i].yaowu + ' &nbsp; ' + util.FormateNull(list[i].yongliang) + ' &nbsp; ' + util.FormateNull(list[i].cankaoyongliang) + ' &nbsp; ' + util.FormateNull(list[i].fenlei) + ' <img src="/images/quxiao.png" alt="" class="quxiaopic" onclick="del(\'' + list[i].yaowu + '\',\'' + list[i].yongliang + '\',\'' + list[i].cankaoyongliang + '\')"> <img src="/images/edit.png" class="quxiaopic" onclick="update(\'' + list[i].yaowu + '\',\'' + list[i].yongliang + '\',\'' + list[i].cankaoyongliang + '\')"></img>';
                if (i != 0) {
                    html += '<img src="/images/up.png" class="quxiaopic" onclick="up(\'' + list[i].sort + '\')"><img>';
                }
                if (i != list.length-1) {
                    html += '<img src="/images/down.png" class="quxiaopic" onclick="down(\'' + list[i].sort + '\')"></img>';
                }
                html += '</span>';
            }
            $("#divPeifang").html(html);
        }
        $("#spnumber").html(list.length);
    }
    function up(sort) {
        for (var i = 0; i < list.length; i++) {
            if (list[i].sort == sort) {
                var tmpsort = list[i].sort;
                list[i].sort = list[i - 1].sort;
                list[i - 1].sort = tmpsort;

                var tmp = list[i-1];
                list[i - 1] = list[i];
                list[i] = tmp;
                break;
            }
        }
        getZhenzhuang();
    }
    function down(sort) {
        for (var i = 0; i < list.length; i++) {
            if (list[i].sort == sort) {
                var tmpsort = list[i].sort;
                list[i].sort = list[i + 1].sort;
                list[i + 1].sort = tmpsort;

                var tmp = list[i + 1];
                list[i + 1] = list[i];
                list[i] = tmp;
                break;
            }
        }
        getZhenzhuang();
    }
    function validate() {
        util.postJson("/fjgl/validate",
            {
                "id": _app_guid,
            }, function (json) {
                if (json.status == 0) {
                    alert(json.msg);
                    location.href = "/fjgl/List";
                } else {
                    alert(json.msg);
                }
            });
    }
    function update(yaowu, yongliang, cankaoyongliang) {
        $("#peifang1").val(yaowu);
        $("#peifang2").val(util.FormateNull(yongliang));
        $("#cankaoyongliang").val(util.FormateNull(cankaoyongliang));
    }
    $(document).ready(function () {
        $("#peifang1").keydown(function (e) {
            var curKey = e.which;
            if (curKey == 13) {
                add();
                return false;
            }
        });
        $("#peifang2").keydown(function (e) {
            var curKey = e.which;
            if (curKey == 13) {
                add();
                return false;
            }
        });
        $("#cankaoyongliang").keydown(function (e) {
            var curKey = e.which;
            if (curKey == 13) {
                add();
                return false;
            }
        });
    });
</script>